package com.framework.modules.sys.controller;


import com.framework.exception.util.RespResult;
import com.framework.page.PageUtils;
import com.framework.service.LogTranInfoService;
import com.framework.service.TransactionInfoService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Map;

/**
 * 交易相关
 * @author wq
 */
@RestController
@RequestMapping("/sys/transactionInfo")
public class TransactionInfoController extends AbstractController {
    @Autowired
    private TransactionInfoService transactionInfoService;

    @Autowired
    private LogTranInfoService logTranInfoService;

    /**
     * 导入excel
     * @param file
     * @return
     */
    @PostMapping("/excel")
    public RespResult putExcel(@RequestParam("file") MultipartFile file){
        Long deptId=getUser().getDeptId();
        String fileName =file.getOriginalFilename();
        Integer count=0;
        try{
            if(deptId.equals(889326L) || deptId.equals(4L)){
                count=transactionInfoService.insertKaoLatran(fileName,file,deptId);
            }else if(deptId.equals(5L)) {
                count=transactionInfoService.insertTLTransaction(fileName, file,deptId);
            }else {
                count=transactionInfoService.insertTransaction(fileName, file,deptId);
            }
        }catch (Exception e) {
            e.printStackTrace();
        }
        return RespResult.ok().put("msg","导入了"+count+"条交易");
    }

    /**
     * 列表
     * @param params
     * @return
     */
    @RequestMapping("/list")
    public RespResult list(@RequestParam Map<String,Object> params){
        params.put("deptId",getDeptId());
        PageUtils page = transactionInfoService.queryTranList(params);
        return RespResult.ok().put("page",page);
    }

    /**
     * 总额
     * @param q
     * @return
     */
    @RequestMapping("/amountDetail")
    public RespResult amountdetail(@RequestBody Map<String,Object> q){
        Long deptId=getUser().getDeptId();
        q.put("deptId",deptId);
        Map<String, Object> result = transactionInfoService.queryPersonAmountDetail(q);
        return RespResult.ok().put("result",result);
    }

    /**
     * 个人详情列表
     * @param params
     * @return
     */
    @RequestMapping("/detail")
    public RespResult personTradeDetail(@RequestParam Map<String,Object> params){
        params.put("deptId",getDeptId());
        PageUtils page=transactionInfoService.queryPersonTradeDetail(params);
        return RespResult.ok().put("page",page);

    }

    /**
     * 交易导入日志列表
     */
    @RequestMapping("/logList")
    public RespResult logList(@RequestParam Map<String,Object> params){
        params.put("deptId",getDeptId());
        PageUtils page=logTranInfoService.queryAutoLogList(params);
        return RespResult.ok().put("page",page);

    }

    /**
     * 查询所有交易总额
     * @param q
     * @return
     */
    @RequestMapping("/queryTotal")
    public RespResult totalAmount(@RequestBody Map<String,Object> q){
        Long deptId=getUser().getDeptId();
        q.put("deptId",deptId);
        String totalAmountStr=transactionInfoService.querytotalAmount(q);
        return RespResult.ok().put("totalAmount",totalAmountStr);

    }

    /**
     * 删除交易
     * @param ids
     * @return
     */
    @RequiresPermissions("sys:transaction:delete")
    @RequestMapping("/deleteTran")
    public RespResult delete(@RequestBody Long [] ids){
        Map<String, Object> map = transactionInfoService.deleteTranByIds(ids);

        return  RespResult.ok(map);
    }



}
